$(document).ready(
		function() {

			$("#submit_btn").bind("click", saveRoom);
			var roomId = $("#hidden_roomId").val();

			var initObj = {
				company : {},
				community : {},
				building : {}
			};
			initObj.company.companyId = $("#sl_community").attr("data-cp-id");
			initObj.company.companyName = $("#sl_community").attr(
					"data-cp-name");
			initObj.company.isNeed = true;
			initObj.community.communityId = $("#sl_community").attr(
					"data-cy-id");
			initObj.community.communityName = $("#sl_community").attr(
					"data-cy-name");
			initObj.community.isNeed = true;
			initObj.building.isNeed = true;
			initObj.handler = s_handler;
			s_init(initObj);
			$("#sl_community").on("click", function() {
				s_show();
			});

			if (!StrUtil.isEmpty(roomId)) {
				getRoom(roomId);
			} else {
				// 获取选择属性
				getSelectValue('upgradeCondition', $("#community").attr(
						"data-id"), '#upgradeCondition');
				getSelectValue('usingState', $("#community").attr("data-id"),
						'#usingState');
				getBuildingTypes($("#community").attr("data-id"));
			}
		});

// 保存物业公司方法
function saveRoom() {

	var buildingId = $("#building").attr("data-id");
	if (buildingId == null || buildingId == null) {
		$.sucmodal("提示", "请选择楼栋");
	}
	var roomNbr = $("#roomNbr").val(); // 房间号
	if (roomNbr == "") {
		$.sucmodal("提示", "房间号必填");
		return;
	}
	var housingFeeRate = $("#housingFeeRate").val(); // 物业折扣率
	/*
	 * if (housingFeeRate == "") { $.sucmodal("提示", "物业折扣率必填"); return; }
	 */
	var partyName = $("#partyName").val(); // 业主
	/*
	 * if (partyName == "") { $.sucmodal("提示", "业主必填"); return; }
	 */
	var userAccount = $("#userAccount").val(); // 注册手机号
	/*
	 * if (userAccount == "") { $.sucmodal("提示", "注册手机号必填"); return; }
	 */

	var buildingTypeId = $("#buildingType option:selected").val(); // 房屋类型
	var statusCd = $("#statusCd option:selected").val();
	var floor = $("#floor").val();// 楼层
	if (StrUtil.isEmpty(floor)) {
		$.sucmodal("提示", "请填写房间所在楼层");
		return;
	}
	var housingOrientation = $("#housingOrientation").val();// 房屋朝
	var usingState = $("#usingState option:selected").val();// 使用状态
	var upgradeCondition = $("#upgradeCondition option:selected").val();// 装修状态
	var upgradeStartDate = $("#upgradeStartDate").val();// 装修起始日期
	var upgradeEndDate = $("#upgradeEndDate").val();// 装修结束日期
	var housedDate = $("#housedDate").val();// 入住时
	var certType = $("#certType").val();// 证件类型
	var certNbr = $("#certNbr").val();// 证件号
	var mobile = $("#mobile").val();// 联系电话
	var homePhone = $("#homePhone").val();// 家庭电话

	var jsarray = new Array();
	var room = {
		community : {},
		building : {},
		buildingType : {},
		roomPartyRel : {}
	};
	var inparam = {
		data : {}
	};
	var partyInfo = {};
	var jsRelArray = new Array();

	room.community.communityId = $("#community").attr("data-id");
	room.roomId = $("#hidden_roomId").val();
	room.building.buildingId = buildingId;
	room.roomNbr = roomNbr;
	room.housingFeeRate = housingFeeRate;
	room.statusCd = statusCd;
	room.buildingType.buildingTypeId = buildingTypeId;
	room.floor = floor;
	room.housingOrientation = housingOrientation;
	room.usingState = usingState;
	room.upgradeCondition = upgradeCondition;
	room.upgradeStartDate = upgradeStartDate;
	room.upgradeEndDate = upgradeEndDate;
	room.housedDate = housedDate;

	// 用户
	if (!StrUtil.isEmpty(userAccount) && StrUtil.isEmpty(partyName)) {
		$.sucmodal("提示", "请填写户主名称");
		isSave = false;
		return false;
	}
	if (!StrUtil.isEmpty(userAccount)) {
		var user = {};
		user.account = userAccount;
		partyInfo.user = user;
	}

	// 业主信息
	var roomPartyRel = {};
	if (!StrUtil.isEmpty(partyName)) {
		partyInfo.partyName = partyName;
		roomPartyRel.partyInfo = partyInfo;
		roomPartyRel.relType = '10';
		jsRelArray.push(roomPartyRel);

	}
	room.roomPartyRel = jsRelArray;
	var hRoomId = $("#hidden_room_id").val()
	if (hRoomId != null && hRoomId != "") {
		room.roomId = hRoomId;
	}
	// inParma.organization.upOrgId="0";//物业公司没有上级组织
	jsarray.push(room);
	inparam.data = jsarray;

	// inParma.address.addressId="312";
	// var pzJson =
	// "{'pp_leader_name':'"+pp_leader_name+"','area_area':'"+area_area+"'}";
	$.post("roomAction!saveRoom.action", {
		inParma : JSON.stringify(inparam)
	}, function(data) {
		data = eval("(" + data + ")");
		if (data.result == "true") {
			$.sucmodal("提示", data.msg, function() {
				location.href = "roomManager.jsp";
			});
		} else {
			alert("保存失败" + data.msg);
		}
	});

}

function getRoom(roomId) {
	var inParma = {};
	inParma.roomId = roomId;
	inParma.qryType = "all";
	$.post("roomAction!getRoom.action", {
		inParma : JSON.stringify(inParma)
	}, function(data) {
		data = eval("(" + data + ")");
		var dataList = data.data;
		if (data != null && data.result == "true") {
			var room = dataList[0];
			if (room != null && room != "") {
				$("#roomNbr").val(room.roomNbr); // 房间号
				$("#housingFeeRate").val(room.housingFeeRate); // 物业折扣率
				$("#statusCd option[value='" + room.statusCd + "']").attr(
						"selected", true);

				$("#floor").val(room.floor);// 楼层
				$("#housingOrientation").val(room.housingOrientation);// 房屋朝
				$("#usingState option[value='" + room.usingState + "']").attr(
						"selected", true);// 使用状态
				$("#upgradeCondition").val(room.upgradeCondition);// 装修状态
				$("#upgradeStartDate").val(room.upgradeStartDate);// 装修起始日期
				$("#upgradeEndDate").val(room.upgradeEndDate);// 装修结束日期
				$("#housedDate").val(room.housedDate);// 入住时
				$("#certType").val(room.certType);// 证件类型
				$("#certNbr").val(room.certNbr);// 证件号
				$("#mobile").val(room.mobile);// 联系电话
				$("#homePhone").val(room.homePhone);// 家庭电话

				var community = room.community;

				if (community != null) {
					$("#community").val(community.communityName);
					$("#community").attr("data-id", community.communityId);
					$("#company").val(community.company.companyName);
					$("#company").attr("data-id", community.company.companyId);
				}
				var unit = room.unit;
				var building = room.building;

				if (unit != null) {
					$("#building").attr("data-id", unit.buildingId);
					$("#building").val(
							building.buildingName + "-" + unit.buildingName);
				} else {
					$("#building").attr("data-id", building.buildingId);
					$("#building").val(building.buildingName);
				}

				getBuildingTypes(community.communityId);
				if (room.buildingType != null) {
					$(
							"#buildingType option[value='"
									+ room.buildingType.buildingTypeId + "']")
							.attr("selected", true);
				}

				if (!StrUtil.isEmpty(room.ownerInfo)) {
					$("#partyName").val(
							StrUtil.strnull(room.ownerInfo.partyName)); // 业主
					$("#userAccount").val(
							StrUtil.strnull(room.ownerInfo.account)); // 注册手机号
				}
				// 获取选择属性
				getSelectValue('upgradeCondition', $("#community").attr(
						"data-id"), '#upgradeCondition');
				getSelectValue('usingState', $("#community").attr("data-id"),
						'#usingState');

			}
		} else if (data.result == "false") {
			$.sucmodal("查询失败", "请于管理员联系" + data.msg, 2000);
		}

	});
}

function s_handler(data) {
	var str = "";
	if (!StrUtil.isEmpty(data.company.companyId)) {
		$("#sl_community").attr("data-cp-id", data.company.companyId);
		$("#sl_community").attr("data-cp-name", data.company.companyName);
		str += data.company.companyName;
	}
	if (!StrUtil.isEmpty(data.community.communityId)) {
		$("#sl_community").attr("data-cy-id", data.community.communityId);
		$("#sl_community").attr("data-cy-name", data.community.communityName);
		str += "-" + data.community.communityName;
	}
	if (!StrUtil.isEmpty(data.building.buildingId)) {
		$("#sl_community").attr("data-bd-id", data.building.buildingId);
		$("#sl_community").attr("data-bd-name", data.building.buildingName);
		str += "-" + data.building.buildingName;
	}
	if (!StrUtil.isEmpty(data.room.roomId)) {
		$("#sl_community").attr("data-rm-id", data.room.roomId);
		$("#sl_community").attr("data-rm-name", data.room.roomNbr);
		str += "-" + data.room.roomNbr;
	}
	// $("#community").val(str);
	clickOk();
}

function clickOk() {
	if ($("#sl_community").attr("data-cy-id") == null
			|| $("#sl_community").attr("data-cy-id") == "null"
			|| typeof ($("#sl_community").attr("data-cy-id")) == "undefined") {
		$.sucmodal("提示", "请选择小区");
		return;
	}
	$("#company").val($("#sl_community").attr("data-cp-name"));
	$("#community").val($("#sl_community").attr("data-cy-name"));

	if (StrUtil.isEmpty($("#community").attr("data-un-id"))) {
		$("#building").text($("#community").attr("data-bd-name"));
	} else {
		$("#building").text(
				$("#community").attr("data-bd-name") + '-'
						+ $("#community").attr("data-un-name"));
	}
	getBuildingTypes($("#community").attr("data-cy-id"));
}

function getBuildingTypes(communityId) {

	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		data : {
			"inParma" : JSON.stringify({
				"communityId" : communityId
			})
		},
		dataType : "json",
		url : "parkingAction!getBuildingType.action",// 请求的action路径
		error : function() {// 请求失败处理函数
			alert('请求失败');
		},
		success : function(data) { // 请求成功后处理函数。
			var ret = eval("(" + data + ")");
			if (ret.result == "true") {
				var opt = "<option value=''>请选择</option>";
				$(ret.data)
						.each(
								function(ix, ite) {
									opt += "<option value='"
											+ ite.buildingTypeId + "'>"
											+ ite.buildingTypeName
											+ "</option>";
								});
				$("#buildingType").html("");
				$("#buildingType").append(opt);
			} else {
				alert(data.msg);
			}
		}
	});
}

function getSelectValue(attrCd, communityId, selectTag) {
	$.post("communityAction!getAttrValue.action", {
		"inParma" : JSON.stringify({
			"attrCd" : attrCd,
			"qryType" : "attrCd",
			"communityId" : communityId
		})
	}, function(data) {
		var ret = eval("(" + data + ")");
		if (ret.result == "true") {
			var opt = "<option value=''>请选择</option>";
			$(ret.data).each(
					function(ix, ite) {
						opt += "<option value='" + ite.attrValueId + "'>"
								+ ite.attrValue + "</option>";
					});
			$(selectTag).html("");
			$(selectTag).append(opt);
		} else {
			alert(data.msg);
		}
	});
}
